$(document).ready(function() {
	var ChannelId=getItems("Channel") != null ? getItems("Channel") : "mychannel";
	var FabricFlag=getItems("networkName");
	var Token=getItems("token");
	var orgId=getItems("orgId");

	if(loginToken){
		// console.log(ChannelId+"---"+FabricFlag)
		$(".Reminder").hide();
		//查询块高度
		var queryChannelInfoUrl = '/api/block/public/queryChannelInfo';
		var queryChannelInfoData = {
			"RequestType": "QueryChannelInfoReq",
			"ChannelId":ChannelId,
			"FabricFlag":FabricFlag
		};
		requestPost(queryChannelInfoUrl, queryChannelInfoData, 0);

		
		//查看节点
		var QueryAllPeerUrl = '/api/block/public/queryAllPeer';
		var QueryAllPeerData = {
			"RequestType": "QueryAllPeerReq",
			"ChannelId": ChannelId,
			"FabricFlag":FabricFlag
		};
		requestPost(QueryAllPeerUrl, QueryAllPeerData, 1);

		//查询交易整数
		var TransactionCountUrl='/api/block/public/queryTransactionCount';
		var TransactionCountoData = {
			"RequestType": "QueryTransactionCountReq",
			"ChannelId": ChannelId,
			"FabricFlag":FabricFlag
		};
		requestPost(TransactionCountUrl,TransactionCountoData, 2);

		//查询智能合约
		var queryInstalledUrl = '/api/block/public/queryChainCodeCount';
		var queryInstalledData = {
			"RequestType": "QueryInstalledChainCodesReq",
			"ChannelId": ChannelId,
			"FabricFlag":FabricFlag,
			"OrgId":orgId
		};
		
		requestPost(queryInstalledUrl,queryInstalledData, 3);
	}else{
		$(".Reminder").show();
		//查询块高度
		var queryChannelInfoUrl = '/api/block/cache/queryChannelInfo';
		var queryChannelInfoData = {
			"RequestType": "QueryChannelInfoReq",
			"ChannelId":ChannelId,
			"FabricFlag":FabricFlag
		};
		requestPost(queryChannelInfoUrl, queryChannelInfoData, 0);

		//查看节点
		var QueryAllPeerUrl = '/api/block/cache/queryAllPeer';
		var QueryAllPeerData = {
			"RequestType": "QueryAllPeerReq",
			"ChannelId": ChannelId,
			"FabricFlag":FabricFlag
		};
		requestPost(QueryAllPeerUrl, QueryAllPeerData, 1);

		//查询交易整数
		var TransactionCountUrl='/api/block/cache/queryTransactionCount';
		var TransactionCountoData = {
			"RequestType": "QueryTransactionCountReq",
			"ChannelId": ChannelId,
			"FabricFlag":FabricFlag
		};
		requestPost(TransactionCountUrl,TransactionCountoData, 2);

		//查询智能合约
		var queryInstalledUrl = '/api/block/cache/queryChainCodeCount';
		var queryInstalledData = {
			"RequestType": "QueryInstalledChainCodesReq",
			"ChannelId": ChannelId,
			"FabricFlag":FabricFlag,
		};
		requestPost(queryInstalledUrl,queryInstalledData, 3);
	}

	function requestPost(url, Data, num) {
		var oData = Data;
		$.ajax({
			type: "POST",
			contentType: "application/json",
			url: url,
			data: JSON.stringify(oData),
			dataType: "json",
			headers:{
				Authorization:Token,
			},
			success: function(data) {
				switch(num) {
					case 0:
						if(data.code==200){
							if(data.status==40101){
								sessionStorage.clear();
								window.location.href="";
							}else{
								var oData=JSON.parse(data.data);
								var oNum=oData.ChannelInfo.Heigth-1;
								if(oNum==-1){
									oNum=0;
								}
								$(".blockNumber").html(oNum);
							}	
						}
						break;
					case 1:
						if(data.code==200){
							var oData=JSON.parse(data.data);
							$(".nodeNumber").html(oData.PeerInfoList.length);
						}
						break;
					case 2:
						if(data.code==200){
							var oData=JSON.parse(data.data);
							var oNum=oData.TransactionIdCount-1;
							if(oNum==-1){
								oNum=0;
							}
							$(".transationNumber").html(oNum);
						}
						break;
					case 3:
						if(data.code==200){
							var oData=JSON.parse(data.data);
							$(".InstalledNumber").html(oData.chainCodeCount);
						}
						break;
					default:
						console.log('------');
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				console.log("调取sns接口出错");
				console.log(XMLHttpRequest.status);
				console.log(XMLHttpRequest.readyState);
				console.log(textStatus);
			}
		});
	}

})